package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.date.DateUtil;
import com.tbit.uqbike.object.pojo.dto.CardUsageDTO;
import com.tbit.uqbike.object.pojo.vo.CardUsageSummaryVO;
import com.tbit.uqbike.object.pojo.vo.CardUsageVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.CardUsageService;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 卡券使用记录
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-12
 */
@RestController
@RequestMapping("/cardUsage")
public class CardUsageController {

    @Resource
    private CardUsageService cardUsageService;


    /**
     * 获取卡券使用记录
     * @param dto dto
     * @return Result
     */
    @GetMapping("/getCardUsage")
    public Result<Object> getCardUsage(CardUsageDTO dto){
        if (Objects.isNull(dto.getBrandId())){
            return Result.error("请选择品牌");
        }
        return cardUsageService.getCardUsage(dto);
    }


    /**
     * 获取卡券记录详情
     * @param orderNO 骑行订单号
     * @return Result
     */
    @GetMapping("/getCardUsageDetails")
    public Result<CardUsageVO> getCardUsageDetails(String orderNO){
        return cardUsageService.getCardUsageDetails(orderNO);
    }


    /**
     * 获取卡券使用记录统计
     * @param dto dto
     * @return Result
     */
    @GetMapping("/getCardUsageSummary")
    public Result<CardUsageSummaryVO> getCardUsageSummary(CardUsageDTO dto){
        return cardUsageService.getCardUsageSummary(dto);
    }




    /**
     * 导出卡券使用记录
     * @param dto dto
     * @return Result
     */
    @GetMapping("/exportCardUsage")
    public Result<Object> exportCardUsage(CardUsageDTO dto){
        if (Objects.isNull(dto.getBrandId())){
            return Result.error("请选择品牌");
        }
        if (Objects.isNull(dto.getStartTime()) || Objects.isNull(dto.getEndTime())){
            return Result.error("时间不能为空");
        }
        // 检查时间范围是否超过6个月
        if (DateUtil.betweenMonth(dto.getStartTime(), dto.getEndTime(), true) > 6) {
            return Result.error("时间范围不能超过6个月");
        }
        List<CardUsageVO> data = (List<CardUsageVO>) cardUsageService.getCardUsage(dto).getData();
        ExcelUtil<CardUsageVO> excel = new ExcelUtil<>(CardUsageVO.class);
        JsonResult jsonResult = excel.exportExcel(data, "卡券使用记录");
        return Result.success(jsonResult.getData());
    }


}
